package com.jswzc.common.api.controller;

import com.alibaba.fastjson.JSONObject;
import com.jswzc.api.service.wxTemplate.WxTemplateServiceI;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * Created by ldd_person on 2017/5/10.
 */
@Controller
@RequestMapping("/wxTemplate")
public class WxTemplateController {

    @Resource(name="wxTemplateService")
    private WxTemplateServiceI wxTemplateServiceI;

    @RequestMapping(value="/send_template",method = RequestMethod.POST)
    public @ResponseBody String sendTemplate(HttpServletRequest request) throws IOException {
        //enctype=application/x-www-form-urlencoded
        JSONObject jsonObject =(JSONObject)JSONObject.parse(request.getParameter("json").toString());
        String accessToken = jsonObject.containsKey("accessToken")?jsonObject.get("accessToken").toString():"";
        String open_id = jsonObject.containsKey("open_id")?jsonObject.get("open_id").toString():"";
        String template_id_short = jsonObject.containsKey("template_id_short")?jsonObject.get("template_id_short").toString():"";
        String app_id=jsonObject.containsKey("app_id")?jsonObject.get("app_id").toString():"";
        String link_url =jsonObject.containsKey("link_url")?jsonObject.get("link_url").toString():"";
        String content = jsonObject.containsKey("content")?jsonObject.get("content").toString():"";

        String template_id = null;
        String result=null;
        try {
           // template_id = wxTemplateServiceI.findTemplateId(app_id,accessToken,template_id_short);
            result = wxTemplateServiceI.sendTemplate(app_id,accessToken, open_id, template_id_short, link_url, content);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }


    @RequestMapping(value="/ceshi",method = RequestMethod.POST)
    public @ResponseBody String ceshi(HttpServletRequest request) throws IOException {
        //enctype=application/x-www-form-urlencoded
String app_id="wx987dd5d0c3053fda";
        String accessToken="hN97O2yhXPuRQbznMQ87P8HKQdEbPvU-RcoeofDdGxvCUYd58NwtQr3TuE4LR103v06dWfBxD9_f_Y7TToiB4evl-e-lri7mmioYBsnla_gGziKpjg8KkrTnBXBlyGIVWISjAGAHTD";
        String  open_id="obisKwebhlsFJOeymuqjjOZ72K6Y";
        String template_id_short="OPENTM201605400";
        String link_url="";
        String content="{\"first\": {\"value\":\"您的商品已发货，请等待签收\",\"color\":\"#000000\"},\"keyword1\": {\"value\":\"测试qaz\",\"color\":\"#000000\"},\"keyword2\": {\"value\":\"123\",\"color\":\"#000000\"},\"keyword3\": {\"value\":\"自己配送\",\"color\":\"#000000\"},\"remark\": {\"value\":\"您的商品已经发货，大概3-5天到货，请注意查收\",\"color\":\"#000000\"}}";

        String result=null;
        try {
            // template_id = wxTemplateServiceI.findTemplateId(app_id,accessToken,template_id_short);
            result = wxTemplateServiceI.sendTemplate(app_id,accessToken, open_id, template_id_short, link_url, content);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }


}
